package com.webserver.controller;

import com.webserver.core.ClientHandler;
import com.webserver.entity.User;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;
import java.net.URISyntaxException;

public class UserController {

    private static File rootDir,staticDir;
    private static File userDir;

    static {
        try {
            rootDir = new File(ClientHandler.class.getClassLoader().getResource(".").toURI());
            staticDir = new File(rootDir,"static");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        userDir = new File("users");
        if(!userDir.exists()){
            userDir.mkdirs();//创建文件夹
        }
    }

    public void login(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //输入不规范
        if(username == null || password == null){
            File file = new File(staticDir,"/myweb/login_info_error.html");
            response.setContentFile(file);
            return;
        }

        File userFile = new File(userDir,username+".obj");

        if(userFile.exists()){//用户名正确
            try (
                    FileInputStream fis = new FileInputStream(userFile);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ){

                //读取注册的信息
                User obj = (User) ois.readObject();

                if(obj.getPassword().equals(password)){
                    //登录成功
                    File file = new File(staticDir,"/myweb/login_success.html");
                    response.setContentFile(file);
                    return;
                }


            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }

        }

        //如果程序走到这里,情况1:用户名没有输入正确，文件不存在
        //              情况2:用户名对了，但是密码不对
        File file = new File(staticDir,"/myweb/login_fail.html");
        response.setContentFile(file);



    }

    public void reg(HttpServletRequest request, HttpServletResponse response){

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");

        if(username == null || password == null || nickname == null || ageStr == null || !ageStr.matches("[0-9]+")){
            File file = new File(staticDir,"/myweb/reg_info_error.html");
            response.setContentFile(file);
            return;//直接结束方法
        }

        int age = Integer.parseInt(ageStr);
        System.out.println(username+","+password+","+nickname+","+age);

        User user = new User(username,password,nickname,age);
        File userFile = new File(userDir,username+".obj");

        //判断是否存在用户信息
        if(userFile.exists()){
            File file = new File(staticDir,"/myweb/have_user.html");
            response.setContentFile(file);
            return;//直接结束方法
        }

        //写出用户obj文件，发送成功网页
        try(
                FileOutputStream fos = new FileOutputStream(userFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            oos.writeObject(user);

            File file = new File(staticDir,"/myweb/reg_success.html");
            response.setContentFile(file);

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
